//
//  YWMeterReadingRecordCell.m
//  maintain
//
//  Created by 郑想 on 16/2/24.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMeterReadingRecordCell.h"
#import "UIView+Extension.h"
#import "YWMacro.h"
#import "YYLabel.h"

#import "YWBuildingManager.h"
#import "YWEquipment.h"
#import "YWEquipmentManager.h"
#import "YWMeterReadingRecord.h"

@interface YWMeterReadingRecordCell ()
@property (nonatomic, strong) YYLabel *locationName;
@property (nonatomic, strong) YYLabel *readDate;
@property (nonatomic, strong) YYLabel *thisReadNumer;
@end

@implementation YWMeterReadingRecordCell

+ (instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString* identifier = @"YWMeterReadingRecordCellIdentifier";
    YWMeterReadingRecordCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YWMeterReadingRecordCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    }
    return self;
}

- (YYLabel*)locationName
{
    if (!_locationName) {
        YYLabel* locationName = [[YYLabel alloc] init];
        locationName.font = kContentFont;
        [self.contentView addSubview:locationName];
        _locationName = locationName;
    }
    return _locationName;
}

- (YYLabel*)readDate
{
    if (!_readDate) {
        YYLabel* readDate = [[YYLabel alloc] init];
        readDate.font = [UIFont systemFontOfSize:13];
        readDate.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:readDate];
        _readDate = readDate;
    }
    return _readDate;
}

- (YYLabel*)thisReadNumer
{
    if (!_thisReadNumer) {
        YYLabel* thisReadNumer = [[YYLabel alloc] init];
        thisReadNumer.font = kContentFont;
        [self.contentView addSubview:thisReadNumer];
        _thisReadNumer = thisReadNumer;
    }
    return _thisReadNumer;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat lPadding = 15.0;
    CGFloat rPadding = 10.0;
    CGFloat readTimeW = 80;
    CGFloat showW = self.width - lPadding - rPadding;
    CGFloat labelH = 25.0;

    self.locationName.frame = CGRectMake(lPadding, rPadding, showW, labelH);
    // self.locationName.backgroundColor = [UIColor greenColor];

    self.thisReadNumer.frame = CGRectMake(lPadding, CGRectGetMaxY(self.locationName.frame), showW - readTimeW, labelH);
    //  self.thisReadNumer.backgroundColor = [UIColor greenColor];

    self.readDate.frame = CGRectMake(CGRectGetMaxX(self.thisReadNumer.frame), CGRectGetMaxY(self.locationName.frame), readTimeW, labelH);
    // self.readDate.backgroundColor = [UIColor redColor];
}

- (void)setMeterReadingRecord:(YWMeterReadingRecord*)meterReadingRecord
{
    _meterReadingRecord = meterReadingRecord;
    YWEquipment* equipment = [[YWEquipmentManager shareManager] equipmentWithId:meterReadingRecord.equipmentId];
    self.locationName.text = [[YWBuildingManager shareManager] equipmentLocation:equipment];
//    CGFloat thisRead = meterReadingRecord.thisReadNumber;
//    YWMeterProperty *meterProperty = [[YWEquipmentManager shareManager] meterPropertyWithEquipmentId:meterReadingRecord.equipmentId];
//    CGFloat useNumber = 0;
//    if (meterProperty.rate>0) {
//        useNumber = (meterReadingRecord.thisReadNumber - meterReadingRecord.lastReadNumber)*meterProperty.rate;
//    }else{
//        useNumber = meterReadingRecord.thisReadNumber - meterReadingRecord.lastReadNumber;
//    }
    NSString *thisReadStr = meterReadingRecord.usedAmount>0?[NSString stringWithFormat:@"本次用量: %.2f",meterReadingRecord.usedAmount]:@"本次用量: -";
    self.thisReadNumer.text = thisReadStr;
    self.readDate.text = meterReadingRecord.dateShow;
}
@end
